package data;

import static org.junit.Assert.assertTrue;

import java.math.BigInteger;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.SecureRandom;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.util.List;

import org.junit.Before;
import org.junit.Test;

import com.google.common.collect.Lists;

public class BlindTest
{
    CardsManager manager = new CardsManager();

    @Before
    public void initialize()
    {
        manager.addCandidate( "1 Jerzy Palaczka" );
        manager.addCandidate( "2 Krzysztof Brzeczyszczykiewicz" );
        manager.addCandidate( "3 Zenon Malinka" );
        manager.addCandidate( "4 Baracki Obamowy" );
        manager.addCandidate( "5 Wladimiriej Putinowski" );
        manager.addCandidate( "6 Bronislaw Komornicki" );
    }

    @Test
    public void initTest()
    {
        for( int i = 0; i < 10000; i++ )
        {
            List<BigInteger> tab;
            tab = Lists.newLinkedList();
            KeyPairGenerator keyGen = null;
            try
            {
                keyGen = KeyPairGenerator.getInstance( "RSA" );
            }
            catch( Exception e )
            {
                System.out.println( e );
            }
            keyGen.initialize( 1024, new SecureRandom() );
            KeyPair keypair = keyGen.genKeyPair();
            RSAPrivateKey privKey = (RSAPrivateKey)keypair.getPrivate();
            RSAPublicKey pubKey = (RSAPublicKey)keypair.getPublic();
            KeyPairGenerator keyGen2 = null;
            try
            {
                keyGen2 = KeyPairGenerator.getInstance( "RSA" );
            }
            catch( Exception e )
            {
                System.out.println( e );
            }
            keyGen2.initialize( 1024, new SecureRandom() );
            KeyPair keypair2 = keyGen2.genKeyPair();
            RSAPrivateKey priv2 = (RSAPrivateKey)keypair2.getPrivate();
            RSAPublicKey pub2 = (RSAPublicKey)keypair2.getPublic();
            Blind b = new Blind();
            List<VotingSet> sets = manager.exportSets();
            String result = b.doBlind( sets.get( 0 ), pubKey, tab );
            System.out.println( result );
            String result2 = b.sign( result, privKey );
            System.out.println( result2 );
            String result3 = b.unblind( result2, 0, pubKey, tab );
            System.out.println( result3 );
            int voteNumber = sets.get( 0 ).getVoteNumber( "5 Wladimiriej Putinowski" );
            String result4 = b.pickCard( result3, voteNumber );
            System.out.println( result4 );
            String temp1 = b.SecHash( result4, pub2 );
            System.out.println( temp1 );
            String temp2 = b.SecUnhash( temp1, priv2 );
            System.out.println( temp2 );
            assertTrue(result4.equals(temp2));
            VotingCard v = b.unsign( temp2, pubKey );
            String result5 = v.toString();
            System.out.println( "--------------" );
            System.out.println( result5 );
            System.out.println( i );
            assertTrue( result5
                    .startsWith( "1 Jerzy Palaczka f\r\n2 Krzysztof Brzeczyszczykiewicz f\r\n3 Zenon Malinka f\r\n4 Baracki Obamowy f\r\n5 Wladimiriej Putinowski t\r\n6 Bronislaw Komornicki f" ) );
        }
    }
}
